{% extends "base.html" %}
{% load event_tags %}
{% load static %}
{% block add_css %}
    {{ block.super }}

    <link rel="stylesheet" href="{% static "easymde/dist/easymde.min.css" %}">
{% endblock %}
{% block add_styles %}
    {{ block.super }}
    .chosen-container {
    width: 70% !important;
    }
    .editor-toolbar, .editor-statusbar, .editor-preview-side, .CodeMirror {
    width: 70% !important;
    }
{% endblock %}
{% block content %}
    {{ block.super }}
    <h3> Apply template to a Finding</h3>
    <form id="add_finding" class="form-horizontal" name="apply_template"
          action="{% url 'apply_template_to_finding' finding.id template.id %}" method="post">
        {% csrf_token %}
        {% include "dojo/apply_finding_template_form_fields.html" with form=form template=template %}
        {% if jform %}
            {% include "dojo/apply_finding_template_form_fields.html" with form=jform template=template %}
        {% endif %}
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <input class="btn btn-primary" name="_Finished" type="submit" value="Finished"/>
            </div>
        </div>
    </form>
{% endblock %}
{% block postscript %}
    {{ block.super }}
    
    <script type="application/javascript" src="{% static "jquery.hotkeys/jquery.hotkeys.js" %}"></script>
   <script type="application/javascript" src="{% static "easymde/dist/easymde.min.js" %}"></script>
    <script type="text/javascript" src="{% static "admin/js/jquery.init.js" %}"></script>
    <script type="application/javascript">
        $ = django.jQuery;
        $.hotkeys.options.filterInputAcceptingElements = false;
        $.hotkeys.options.filterTextInputs = false;

        $(function () {
            $("textarea").each(function (index, elem) {
                if (elem.hasAttribute("required")) {
                    elem.removeAttribute("required");
                    elem.id = "req"
                }

                if (elem.name != 'vulnerability_ids' && !$(elem).hasClass('select2-search__field')) {
                    var mde = new EasyMDE({
                        spellChecker: false,
                        inputStyle: "contenteditable",
                        element: elem,
                        autofocus: false,
                        forceSync: true,
                        toolbar: ["bold", "italic", "heading", "|",
                            "quote", "unordered-list", "ordered-list", "|",
                            "link", "image", "|",
                            "table", "horizontal-rule", "code", "|",
                            "guide"
                        ]
                    });
                    mde.render();
                    elem.mde = mde;
                }
            });
        });

        $(document).bind('keydown', 'ctrl+s', function (event) {
            if (event.preventDefault) {
                event.preventDefault();
            } else {
                // internet explorer
                event.returnValue = false;
            }
            $("input[name='_Finished']").trigger('click');
        });

        $("#add_finding").submit(function () {
            var isFormValid = true;

            $("textarea#req").each(function () {
                if ($.trim($(this).val()).length == 0) {
                    $(this).addClass("highlight");
                    isFormValid = false;
                }
                else {
                    $(this).removeClass("highlight");
                }
            });

            if (!isFormValid) alert("Please fill in all the required fields (indicated by *)");

            return isFormValid;
        });
    </script>
    <script>
        $("#template_set_all_options button").click(function () {
            $("#template_set_all_options button").removeClass("btn-primary");
            $(this).addClass("btn-primary");
            var option = $(this).data('option');
            $("#add_finding select option[value='" + option + "']").prop("selected", true).change();
            return false;
        });
    </script>
{% endblock %}
